package com.jvtd.eyl.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.AttributeSet;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jvtd.eyl.R;
import com.jvtd.eyl.bean.binding.BookBean;
import com.jvtd.eyl.bean.binding.BrowseBean;
import com.jvtd.eyl.constants.App;
import com.jvtd.eyl.ui.main.my.browse.BrowseActivity;
import com.jvtd.eyl.utils.ReadUtils;
import com.jvtd.eyl.widget.adapter.BrowseAdapter;
import com.jvtd.widget.emptyView.JvtdEmptyView;
import com.jvtd.widget.recyclerView.JvtdRecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 浏览列表
 * 作者:chenlei
 * 时间:2018/11/14 1:27 PM
 */
public class BrowseRecyclerView extends JvtdRecyclerView<BrowseBean> {
    private boolean isLimit = false;
    private MyFooterView mFooterView;

    public BrowseRecyclerView(@NonNull Context context) {
        super(context);
    }

    public BrowseRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public BrowseRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public BaseQuickAdapter initAdapter() {
        BrowseAdapter mAdapter = new BrowseAdapter();
//        mAdapter.openLoadAnimation();
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            List<BookBean> bookBeans = new ArrayList<>();
            for (BrowseBean browseBean : mAdapter.getData()) {
                bookBeans.add(browseBean.getDocumentperiodical());
            }
            ReadUtils.readFile(mContext,bookBeans,position);
        });
        return mAdapter;
    }

    @Override
    public double getFlingRatio()
    {
        return App.FLING_RATIO;
    }

    @Override
    public LayoutManager initLayoutManager() {
        return new LinearLayoutManager(mContext);
    }

    @Override
    public boolean isFirst() {
        return true;
    }

    @Override
    public int getLoadNum() {
        return 0;
    }

    @Override
    protected void configEmptyView(JvtdEmptyView emptyView) {
        emptyView.setEmptyImage(0)
                .setErrorText(R.string.loading_failed_touch)
                .setErrorImage(0)
                .setLoadingText("")
                .setEmptyText("");
    }

    /**
     * 开始数量限制
     * @param limit 是与否
     */
    public void setLimit(boolean limit) {
        isLimit = limit;
        if (limit){
            mFooterView = new MyFooterView(mContext);
            mFooterView.setOnClickListener(v -> {
                mContext.startActivity(BrowseActivity.getIntent(mContext));
            });
            Objects.requireNonNull(getAdapter()).addFooterView(mFooterView);
        }else {
            if (mFooterView != null)
                Objects.requireNonNull(getAdapter()).removeAllFooterView();
        }
    }

    @Override
    public JvtdRecyclerView setData(List<BrowseBean> data) {
        if (!isLimit)
            return super.setData(data);
        else {
            int num = Math.min(data.size(), 5);
            if (num < 5){
                mFooterView.setVisibility(GONE);
            }else
                mFooterView.setVisibility(VISIBLE);
            return super.setData(data.subList(0, num));
        }
    }
}
